const  Ws = require('ws');

;((Ws)=>{
    const server = new Ws.Server({ port: 9000 });
    console.log("后端已启动")


    const init =()=>{
        bindEvent()
    }

    function bindEvent(){
        server.on('open',handleOpen);
        server.on('close',handleClose);
        server.on('connection',handleConnection);
        server.on("error",handleError);
    }
    function handleOpen(){
        console.log("后端 websoket open")
    }
    function handleClose(){
        console.log("后端 Websoket close")
    }
    function handleError(){
        console.log("后端 websoket error")
    }
    function handleConnection(ws){
        console.log("后端  websoket connection")
        ws.on('message',handleMessage)
    }
    function handleMessage(msg){
        console.log("前端 websoket message:%s",msg)
        server.clients.forEach((c)=>{
            c.send(msg)
        })
    }
    init()
})(Ws);